package com.main.join;

import javax.swing.*;
import java.awt.*;

/**
 * @author Zhang Guangyong
 * @desacription 测试线程join方法
 * @date 2019年07月10日 13:52
 */
public class JoinTest extends JFrame {

    private Thread threadA;
    private Thread threadB;
    private Thread threadC;

    final JProgressBar progressBar = new JProgressBar();
    final JProgressBar progressBar2 = new JProgressBar();
    final JProgressBar progressBar3 = new JProgressBar();

    public JoinTest() {
        super();
        getContentPane().add(progressBar, BorderLayout.NORTH);
        getContentPane().add(progressBar2, BorderLayout.SOUTH);
        getContentPane().add(progressBar3, BorderLayout.CENTER);

        progressBar.setStringPainted(true); // 设置进度条显示数字字符
        progressBar2.setStringPainted(true); // 设置进度条显示数字字符
        progressBar3.setStringPainted(true); // 设置进度条显示数字字符

        // 进度条1 数值
        threadA = new Thread(new Runnable() {
            int count = 0;

            @Override
            public void run() {
                while (true) {
                    progressBar.setValue(++count);
                    try {
                        Thread.sleep(100);
                        threadB.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        threadA.start();


        // 进度条2 数值
        threadB = new Thread(new Runnable() {
            int count = 0;

            @Override
            public void run() {
                while (true) {
                    progressBar2.setValue(++count);
                    try {
                        Thread.sleep(100);
                        threadC.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if (count == 100) {
                        break;
                    }
                }
            }
        });
        threadB.start();

        // 进度条3 数值
        threadC = new Thread(new Runnable() {
            int count = 0;

            @Override
            public void run() {
                while (true) {
                    progressBar3.setValue(++count);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if (count == 100) {
                        break;
                    }
                }
            }
        });
        threadC.start();
    }


    public static void init(JFrame frame, int width, int height) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("测试线程join");
        frame.setSize(width, height);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        init(new JoinTest(), 400, 200);
    }


}
